# File：01 mysql连接.py
# Author：信思智学
# Date ：2025/7/17 14:37
# Info：python+mysql ======>   pymysql
import pymysql

# 创建数据库连接  connection
conn = pymysql.connect(
    user="root",
    password="root",
    host="127.0.0.1",
    port=3306,
    database="spt2505"
)

# 通过数据库连接创建数据库对象
cur = conn.cursor()

username = input("请输入用户名")
password = input("请输入密码")

# 准备sql语句  登录


# 参数替换方式一：
# sql = "select * from t_user where username = '"+username+"' and password = '"+password+"'"
# sql = "select * from t_user where id = "+id+" and username = '"+username+"'"
# print(sql)


# 参数替换方式二：  容易会有sql注入问题   lufei ' or 1=1 -- '   sql注入
sql = "select * from t_user where username = '%s' and password = '%s'"%(username,password)
print(sql)

# 通过数据库对象执行sql
count = cur.execute(sql)
result = cur.fetchall()

print(result)
if result:
    print("登录成功")
else:
    print("用户名或密码错误！")

# 关闭数据库对象
cur.close()
# 关闭数据库连接
conn.close()